
#include "TFMenuView.h"
#include "TFGraphicsManager.h"
#include "TFSoundManager.h"


TFMenuView::TFMenuView()
{
	_menu = NULL;
}

TFMenuView::~TFMenuView()
{
	_menu = NULL;
}


HRESULT TFMenuView::Redraw()
{
	HRESULT HR = S_OK;

	//Get information from the menu model
	//Draw correct menu on the screen
	TF_MENU_MODE menuMode = _menu->GetMenuMode();
	if(menuMode == MAIN_MENU)
		DisplayMenu();
	else if(menuMode == OPTIONS_MENU)
		DisplayOptionsMenu();
	else if(menuMode == CREDITS_MENU)
		DisplayCredits();
	else if(menuMode == HELP_MENU)
		DisplayHelpMenu();
	
	return HR;
}

/*Displays all menus and handles input to the menu. */
HRESULT TFMenuView::DisplayMenu(){
	HRESULT HR = S_OK;

    //Handles input to menu.
	while(_model->GetGameMode() == MENU)
	{
        //Create the menu, load each item sprite, load a selector sprite.
        TFMenu* mainMenu = new TFMenu();
        int single = mainMenu->AddMenuItem((void*)menub_oneplayer_Sprite, (unsigned short*)menub_Pal);
        int twoplayer = mainMenu->AddMenuItem((void*)menub_2player_Sprite, (unsigned short*)menub_Pal);
        int options = mainMenu->AddMenuItem((void*)menub_options_Sprite, (unsigned short*)menub_Pal);
        int help = mainMenu->AddMenuItem((void*)menub_help_Sprite, (unsigned short*)menub_Pal);
        int credits = mainMenu->AddMenuItem((void*)menub_credits_Sprite, (unsigned short*)menub_Pal);
        mainMenu->AddMenuSelect((void*)menub_selector_Sprite,(unsigned short*) menub_Pal, 1);
        mainMenu->Display();

		bool stylusPressed = false;
		int currentSelected = 0;
		int stylusSelect = 0;

        while(_model->GetGameMode() == MENU)
        {           
			if (Stylus.Newpress){
				currentSelected = mainMenu->GetSelected();
				stylusSelect = mainMenu->StylusSelect(Stylus.X, Stylus.Y);
				if(stylusSelect == currentSelected)
				{
					stylusPressed = true;
				}
				else
				{
					TFSoundManager::PlaySound(SOUND_MENU_MOVE);
				}
			}
			
			if(Pad.Newpress.Up)
            {
                mainMenu->ChangeSelected(0);
                TFSoundManager::PlaySound(SOUND_MENU_MOVE);
            }
            else if(Pad.Newpress.Down)
            {
                mainMenu->ChangeSelected(1);
                TFSoundManager::PlaySound(SOUND_MENU_MOVE);
            }
            else if(Pad.Newpress.Start || Pad.Newpress.A || stylusPressed)
            {
				stylusPressed = false;
				TFSoundManager::PlaySound(SOUND_MENU_SELECT);

                int selected = mainMenu->GetSelected();

                if(selected == single)
                {
                    //One player game against AI
					delete mainMenu;
					_model->SetGameMode(GAME_ONEPLAYER);
					return HR;
                }
                else if(selected == twoplayer)
                {
                    //Two player game
					delete mainMenu;
					_model->SetGameMode(GAME_TWOPLAYER);
					return HR;
                }
                else if(selected == options)
                {
                    //Go to options menu;
                    delete mainMenu;
                    DisplayOptionsMenu();
                    break;
                }
                else if(selected == help)
                {
                    //Go to help menu;
					delete mainMenu;
                    DisplayHelpMenu();
                    break;
                }
                else if(selected == credits)
                {
                    //show credits
                    delete mainMenu;
                    DisplayCredits();
                    break;
                }
            }

            PA_WaitForVBL();
        }
    }

	return HR;
}

HRESULT TFMenuView::DisplayOptionsMenu()
{
	bool musicOn = TFSoundManager::isMusicEnabled();
	bool soundOn = TFSoundManager::isSoundEnabled();
	bool rumbleOn = TFSoundManager::isRumbleEnabled();

    //Create the menu, load each item sprite, load a selector sprite.
    TFMenu* optionsMenu = new TFMenu();

	//For toggling sound effects.. 
    int soundFX;
	if (soundOn)
	{
		soundFX = optionsMenu->AddMenuItem((void*)menub_soundon_Sprite, (unsigned short*)menub2_Pal);
	}
	else
	{
		soundFX = optionsMenu->AddMenuItem((void*)menub_soundoff_Sprite, (unsigned short*)menub2_Pal);
	}
	
	//Rumble option for menu (if you have a Rumble pak inserted)
	int rumble;
	if (rumbleOn)
	{
		rumble = optionsMenu->AddMenuItem((void*)menub_rumbleon_Sprite, (unsigned short*)menub_rumbleon_Pal);
	}
	else
	{
		rumble = optionsMenu->AddMenuItem((void*)menub_rumbleoff_Sprite, (unsigned short*)menub_rumbleoff_Pal);
	}

	//We have no music yet therefore no music option
    int music = 999;//optionsMenu->AddMenuItem((void*)menub_musicon_Sprite, (unsigned short*)menub2_Pal);

    int back = optionsMenu->AddMenuItem((void*)menub_back_Sprite, (unsigned short*)menub2_Pal);
    optionsMenu->AddMenuSelect((void*)menub_selector_Sprite,(unsigned short*) menub_Pal, 1);
    optionsMenu->Display();

	bool stylusPressed = false;
	int currentSelected = 0;
	int stylusSelect = 0;
    while(1)
    {
		if (Stylus.Newpress){
			currentSelected = optionsMenu->GetSelected();
			stylusSelect = optionsMenu->StylusSelect(Stylus.X, Stylus.Y);
			if(stylusSelect == currentSelected)
			{
				stylusPressed = true;
			}
			else
			{
				TFSoundManager::PlaySound(SOUND_MENU_MOVE);
			}
		}
		//To go back with "B" if not touching screen
		else if (Pad.Newpress.B)
		{
			optionsMenu->SetSelectedElement(back);
			stylusPressed = true;
		}

		if (Pad.Newpress.Up)
        {
            optionsMenu->ChangeSelected(0);
            TFSoundManager::PlaySound(SOUND_MENU_MOVE);
        }
        else if (Pad.Newpress.Down)
        {
            optionsMenu->ChangeSelected(1);
            TFSoundManager::PlaySound(SOUND_MENU_MOVE);
        }
		else if(Pad.Newpress.Start || Pad.Newpress.A || stylusPressed)
        {
			stylusPressed = false;

			TFSoundManager::PlaySound(SOUND_MENU_SELECT);
            
            int selected = optionsMenu->GetSelected();
            
            if(selected == back)
            {
                delete optionsMenu;
                return S_OK;
            }
			if(selected == soundFX){
				if(soundOn){
					optionsMenu->ChangeSprite((void*)menub_soundoff_Sprite, (unsigned short*)menub2_Pal, selected);
					soundOn = false;
				}else{
					optionsMenu->ChangeSprite((void*)menub_soundon_Sprite, (unsigned short*)menub2_Pal, selected);
					soundOn = true;
				}
				TFSoundManager::setSoundEnabled(soundOn);
			}
			if(selected == rumble){
				if(rumbleOn){
					optionsMenu->ChangeSprite((void*)menub_rumbleoff_Sprite, (unsigned short*)menub_rumbleoff_Pal, selected);
					rumbleOn = false;
				}else{
					optionsMenu->ChangeSprite((void*)menub_rumbleon_Sprite, (unsigned short*)menub_rumbleon_Pal, selected);
					rumbleOn = true;
				}
				TFSoundManager::setRumbleEnabled(rumbleOn);
			}
			if(selected == music){
				musicOn = TFSoundManager::isMusicEnabled();
				if(musicOn){
					optionsMenu->ChangeSprite((void*)menub_musicoff_Sprite, (unsigned short*)menub2_Pal, selected);
					musicOn = false;
				}else{
					optionsMenu->ChangeSprite((void*)menub_musicon_Sprite, (unsigned short*)menub2_Pal, selected);
					musicOn = true;
				}
				TFSoundManager::setMusicEnabled(musicOn);
			}
        }
        PA_WaitForVBL();
    }
    return E_FAIL;
}

HRESULT TFMenuView::DisplayHelpMenu()
{
	//Create the menu, load each item sprite, load a selector sprite.
	TFMenu* helpMenu = new TFMenu();
	int controls = helpMenu->AddMenuItem((void*)menub_controls_Sprite, (unsigned short*)menub3_Pal);
	int howtoplay = helpMenu->AddMenuItem((void*)menub_rules_Sprite, (unsigned short*)menub_rules_Pal);
	int back = helpMenu->AddMenuItem((void*)menub_back_Sprite, (unsigned short*)menub2_Pal);
	helpMenu->AddMenuSelect((void*)menub_selector_Sprite,(unsigned short*) menub_Pal, 1);
	helpMenu->Display();

	bool stylusPressed = false;
	int currentSelected = 0;

	while(1)
	{
		if (Stylus.Newpress){
			currentSelected = helpMenu->GetSelected();
			int stylusSelect = helpMenu->StylusSelect(Stylus.X, Stylus.Y);
			if(stylusSelect == currentSelected)
			{
				stylusPressed = true;
			}
			else
			{
				TFSoundManager::PlaySound(SOUND_MENU_MOVE);
			}
		}
		//To go back with "B" if not touching screen
		else if (Pad.Newpress.B)
		{
			helpMenu->SetSelectedElement(back);
			stylusPressed = true;
		}


		if (Pad.Newpress.Up)
		{
			helpMenu->ChangeSelected(0);
			TFSoundManager::PlaySound(SOUND_MENU_MOVE);
		}
		else if (Pad.Newpress.Down)
		{
			helpMenu->ChangeSelected(1);
			TFSoundManager::PlaySound(SOUND_MENU_MOVE);
		}
		else if(Pad.Newpress.Start || Pad.Newpress.A || stylusPressed)
		{
			stylusPressed = false;
			TFSoundManager::PlaySound(SOUND_MENU_SELECT);
            
			int selected = helpMenu->GetSelected();
            
			if(selected == back)
			{
				delete helpMenu;
				return S_OK;
			}
			else if (selected == controls)
			{
				helpMenu->HideMenu();
				DisplayControls();
				helpMenu->ShowMenu();
			}
			else if (selected == howtoplay)
			{
				helpMenu->HideMenu();
				DisplayHowToPlay();
				helpMenu->ShowMenu();
			}
		}
		PA_WaitForVBL();
	}
    return E_FAIL;
}

HRESULT TFMenuView::DisplayOnePlayer()
{
	PA_OutputText(0, 5,5, "Single player game");
	PA_OutputText(0, 5,6, "Touch to go back");
	while(!Pad.Newpress.Start && !Pad.Newpress.B && !Stylus.Newpress)
	{
		PA_WaitForVBL();
	}
	PA_ClearTextBg(0);
	return S_OK;
}

HRESULT TFMenuView::DisplayTwoPlayer()
{
	PA_OutputText(0, 5,5, "Two player game");
	PA_OutputText(0, 5,6, "Touch to go back");
	while(!Pad.Newpress.Start && !Pad.Newpress.B && !Stylus.Newpress)
	{
		PA_WaitForVBL();
	}
	PA_ClearTextBg(0);
	return S_OK;
}

HRESULT TFMenuView::DisplayControls()
{
	PA_EasyBgLoad(0, 3, controls);
	while(!Pad.Newpress.Start && !Pad.Newpress.B && !Stylus.Newpress)
	{
		PA_WaitForVBL();
	}
	PA_EasyBgLoad(0, 3, troopsLogo_bottom);
	return S_OK;
}

HRESULT TFMenuView::DisplayHowToPlay()
{
	PA_EasyBgLoad(0, 3, howToPlay);
	
	//PA_LoadSpritePal(0, // Screen
	//		7, // Palette number
	//		(void*)dpad_Pal);	// Palette name
	//				
	//PA_CreateSprite(0, // Screen
	//		5, // Sprite number
	//		(void*)dpad_Sprite, // Sprite name
	//		OBJ_SIZE_32X32, // Sprite size
	//		1, // 256 color mode
	//		7, // Sprite palette number
	//		5, 5); // X and Y position on the screen

	howtoScrollValue = 0;

	while(!Pad.Newpress.Start && !Pad.Newpress.B)
	{		
		if (Stylus.Newpress)
		{
			oldStylusX = Stylus.X;
			oldStylusY = Stylus.Y;
			oldScrollValue = howtoScrollValue;
		}
		else if (Stylus.Held)
		{
			howtoScrollValue = oldScrollValue + (oldStylusY - Stylus.Y);
		}
		howtoScrollValue += (Pad.Held.Down - Pad.Held.Up)*4;
		if(howtoScrollValue < 0) {
			howtoScrollValue = 0;
		}
		else if(howtoScrollValue > 1200) {
			howtoScrollValue = 1200;
		}
		
		PA_LargeScrollY(0, 3, howtoScrollValue);
		PA_WaitForVBL();
	}
	PA_EasyBgLoad(0, 3, troopsLogo_bottom);
	//PA_DeleteSprite(0, 5);
	return S_OK;
}

HRESULT TFMenuView::DisplayCredits()
{
	PA_EasyBgLoad(1, 3, credits_top);
	PA_EasyBgLoad(0, 3, credits_bottom);

	while(!Pad.Newpress.Start && !Pad.Newpress.B && !Stylus.Newpress)
	{
		PA_WaitForVBL();
	}

	PA_EasyBgLoad(1, 3, troopsLogo);
	PA_EasyBgLoad(0, 3, troopsLogo_bottom);
	return S_OK;
}
